package top.zimang.loveforwords.Handler;

import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.util.Log;

import org.xml.sax.Attributes;
import org.xml.sax.SAXException;
import org.xml.sax.helpers.DefaultHandler;

import java.util.ArrayList;
import java.util.List;

import top.zimang.loveforwords.Model.Acceptation;
import top.zimang.loveforwords.Model.ExampleSentence;
import top.zimang.loveforwords.Model.Word;


public class MyHandlerSAX extends DefaultHandler {
    private String nodeName;
    private String key;
    private String pos;
    private String exa;
    private String exaTran;
//    Word
    private StringBuilder keyVar;
    private StringBuilder psVar;
    private StringBuilder pronVar;
//    Acceptation
    private StringBuilder meaningVar;
    private StringBuilder posVar;
//    ExampleSentence
    private StringBuilder exaVar;
    private StringBuilder exaTransVar;

    List<ExampleSentence> sent;
    List<Acceptation> means;
    List<String> ps;
    List<String> pron;

    public void setPs(List<String> ps) {
        this.ps = ps;
    }

    public void setPron(List<String> pron) {
        this.pron = pron;
    }

    public void setSent(List<ExampleSentence> sent) {
        this.sent = sent;
    }

    public void setMeans(List<Acceptation> means) {
        this.means = means;
    }

    private Word parserWord;
    public Word getParserWord() {
        return parserWord;
    }

    @Override
    public void startDocument() throws SAXException {
        super.startDocument();

        keyVar=new StringBuilder();
        psVar=new StringBuilder();
        pronVar=new StringBuilder();

        meaningVar=new StringBuilder();
        posVar=new StringBuilder();

        exaVar=new StringBuilder();
        exaTransVar=new StringBuilder();

    }

    @Override
    public void startElement(String uri, String localName, String qName, Attributes attributes) throws SAXException {
        super.startElement(uri, localName, qName, attributes);
        nodeName=localName;
    }

    @Override
    public void characters(char[] ch, int start, int length) throws SAXException {
//        根据节点名称添加内容
        super.characters(ch, start, length);
        if("key".equals(nodeName)){
            keyVar.append(ch,start,length);
        }else if ("ps".equals(nodeName)){
            psVar.append(ch,start,length);
        }else if ("pron".equals(nodeName)){
            pronVar.append(ch,start,length);
        }else if ("pos".equals(nodeName)){
            posVar.append(ch,start,length);
        }else if ("acceptation".equals(nodeName)){
            meaningVar.append(ch,start,length);
        }else if ("orig".equals(nodeName)){
            exaVar.append(ch,start,length);
        }else if ("trans".equals(nodeName)){
            exaTransVar.append(ch,start,length);
        }
    }

    @Override
    public void endElement(String uri, String localName, String qName) throws SAXException {
        if ("dict".equals(localName)){
            parserWord=new Word(key,ps,pron,sent,means);
        }else if ("key".equals(nodeName)){
            key=keyVar.toString().trim();
            keyVar.setLength(0);
        }else if ("ps".equals(nodeName)){
            ps.add(psVar.toString().trim());
            psVar.setLength(0);
        }else if ("pron".equals(nodeName)){
            pron.add(pronVar.toString().trim());
            pronVar.setLength(0);
        }else if ("pos".equals(nodeName)){
            pos=posVar.toString().trim();
            posVar.setLength(0);
        }else if ("acceptation".equals(nodeName)){
            means.add(new Acceptation(meaningVar.toString().trim(),pos));
            meaningVar.setLength(0);
        }else if ("orig".equals(nodeName)){
            exa=exaVar.toString().trim();
            exaVar.setLength(0);
        }else if ("trans".equals(nodeName)){
            exaTran=exaTransVar.toString().trim();
            exaTransVar.setLength(0);

            ExampleSentence exampleSentence=new ExampleSentence(exa,exaTran);
            sent.add(exampleSentence);
        }
    }

    @Override
    public void endDocument() throws SAXException {
        super.endDocument();
    }
}
